﻿using System;
using System.Windows.Forms;

namespace WindowsFormsApp_TestLibuECC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Console.WriteLine("LibuECCHelper.Get_version : " + LibuECCHelper.Get_version());
            //byte[] private_key = new byte[32];

            //for (int i = 0; i < 32; i++)
            //{
            //    private_key[i] = (byte)((int)'0' + i);
            //}
            //string private_key_base64 = Convert.ToBase64String(private_key);
            #region sign
            string private_key_base64 = "MDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk8=";// Convert.ToBase64String(private_key);
            string public_key_base64 = "jRKrmbtiCk6ByPoZ+U3RgL81Y6fXBOtkM9wxPKzYg0pAXSm2WT2aIe1ndiQGsnEmuuisyDEVRWvnNSMbJOMv7g==";

            LibuECCHelper SignECC = new LibuECCHelper();

            Console.WriteLine("Load with Base64 private key: " + private_key_base64);
            SignECC.ModifyPrivateKeyFromBase64(private_key_base64);
            //uECC.ReFreshPublicKey();      //load private key will auto fresh public key

            string message_test = "test message and will be signed";
            string base64_sign = SignECC.Sign(message_test);
            Console.WriteLine("Signed Out Data: " + base64_sign);
            public_key_base64 = SignECC.ReadOutPublicKey();
            Console.WriteLine("Verify Public Key: " + public_key_base64);
            #endregion

            #region verify
            LibuECCHelper verifyECC = new LibuECCHelper();
            verifyECC.ModifyPublicKeyFromBase64(public_key_base64);

            Console.WriteLine("Check with correct Message: " + message_test);
            bool isValid = verifyECC.Verify(message_test, base64_sign);
            Console.WriteLine("Sign Verify:" + isValid.ToString());

            string message_test_fail = "test message and will be signed ";
            //private_key[0] = 0;
            //verifyECC.PrivateKey = private_key;
            //verifyECC.ReFreshPublicKey();
            Console.WriteLine("Check with wrong Message: " + message_test_fail);
            isValid = verifyECC.Verify(message_test_fail, base64_sign);
            Console.WriteLine("Sign Verify:" + isValid.ToString());
            #endregion

            #region 2nd private key get share from 1st
            LibuECCHelper remoteECC = new LibuECCHelper();
            remoteECC.ReGenerateKeyPair();
            string remotePublicKey = remoteECC.ReadOutPublicKey();
            //Console.WriteLine("2nd PrivateKey:" + remoteECC.ReadOutPrivateKey());
            Console.WriteLine("2nd PubulicKey:" + remotePublicKey);
            var shared_secret = remoteECC.GenSharedSecret(public_key_base64);
            Console.WriteLine("2nd Private and 1st Public Shared with: " + remoteECC.ReadOutSharedSecret());
            #endregion

            #region 2nd pub key to 1st private and shared
            SignECC.GenSharedSecret(remotePublicKey);
            Console.WriteLine("1nd Private and 2st Public Shared with: " + SignECC.ReadOutSharedSecret());
            #endregion


        }
    }
}
